package com.ityu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ityu.exception.InsertSameException;
import com.ityu.mapper.EmpMapper;
import com.ityu.mapper.ExprMapper;
import com.ityu.pojo.Emp;
import com.ityu.pojo.Expr;
import com.ityu.pojo.LoginInfo;
import com.ityu.pojo.pagePojo.EmpPage;
import com.ityu.pojo.result.PageResult;
import com.ityu.service.EmpService;
import com.ityu.utlis.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    EmpMapper empMapper;

    @Autowired
    ExprMapper exprMapper;

    @Transactional
    @Override
    public void addEmp(Emp emp) {
        log.info("-----EmpService-----添加员工，参数为{}",emp);
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        try {
            empMapper.addEmp(emp);
        } catch (DuplicateKeyException e) {
            log.error("插入重复员工名称{}" ,emp.getName());
            throw new InsertSameException("员工数据不合法或重复，请重试！");
        }

        if (!CollectionUtils.isEmpty(emp.getExprList())){
            for (Expr expr : emp.getExprList()){
                expr.setEmpId(emp.getId());
            }
            exprMapper.bachAddExpr(emp.getExprList());
        }
    }

    @Override
    public PageResult<Emp> pageListEmp(EmpPage empPage) {
        log.info("-----EmpService-----条件分页查询员工，参数为{}",empPage);
        PageHelper.startPage(empPage.getPage(),empPage.getPageSize());
        Page<Emp> result = (Page<Emp>) empMapper.pageListEmp(empPage);
        return new PageResult<Emp>(result.getTotal(),result.getResult() );
    }

    @Override
    public void deleteEmps(List<Integer> ids) {
        log.info("-----EmpService-----批量删除员工，参数为{}",ids);
        empMapper.deleteEmps(ids);
        exprMapper.deleteExprs(ids);
    }

    @Override
    public Emp getEmpById(Integer id) {
        log.info("-----EmpService-----根据id查询员工，参数为{}",id);
        System.out.println(empMapper.getEmpById(id));
        return empMapper.getEmpById(id);
    }

    @Override
    public void updateEmp(Emp emp) {
        log.info("-----EmpService-----修改员工，参数为{}",emp);
        emp.setUpdateTime(LocalDateTime.now());
        try {
            empMapper.updateEmp(emp);
        } catch (DuplicateKeyException e) {
            log.error("修改重复员工名称{}" ,emp.getName());
            throw new InsertSameException("员工数据不合法或重复，请重试！");
        }

        if (!CollectionUtils.isEmpty(emp.getExprList())){
            exprMapper.deleteAll(emp.getId());
            for (Expr expr : emp.getExprList()){
                expr.setEmpId(emp.getId());
            }
                exprMapper.bachAddExpr(emp.getExprList());
        }
    }

    @Override
    public List<Emp> listEmp() {
        log.info("-----EmpService-----查询所有员工");
        return empMapper.listEmp();
    }


    @Override
    public LoginInfo login(Emp emp) {
        Emp e =   empMapper.getByUsername(emp);
        if (e !=  null){
            Map<String,Object> claims = new HashMap<>();
            log.info("-----EmpServiceLogin-----员工登陆，开始生成JWT令牌");
            claims.put("id",e.getId());
            claims.put("username",e.getUsername());
            claims.put("name",e.getName());
            String token = JwtUtils.generateJwt(claims);

            return new LoginInfo(e.getId(),e.getUsername(),e.getName(),token);
        }else return null;

    }
}
